package com.example.demo.service.impl;

import com.example.demo.constant.SystemConstant;
import com.example.demo.constant.TaskConstant;
import com.example.demo.dto.CheckinRequestDTO;
import com.example.demo.dto.ItemDTO;
import com.example.demo.mapper.CheckinAuditorMapper;
import com.example.demo.mapper.StudentTaskMapper;
import com.example.demo.mapper.entity.CheckinImage;
import com.example.demo.mapper.entity.Item;
import com.example.demo.mapper.entity.StudentTask;
import com.example.demo.mapper.entity.Task;
import com.example.demo.service.CheckinAuditorService;
import com.example.demo.utils.PageBo;
import com.example.demo.utils.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Lucy
 * @create 2024-03-15 14:02
 */
@Service
public class CheckinAuditorServiceImpl implements CheckinAuditorService {

    @Autowired
    private CheckinAuditorMapper mapper;

    @Autowired
    private StudentTaskMapper taskMapper;

    @Override
    public PageVo<CheckinRequestDTO> getCheckinList(PageBo pageBo) {
        List<CheckinRequestDTO> logs = new ArrayList<>();
        List<StudentTask> studentTaskList = mapper.getPendingReviewStudentTasks();
        for(StudentTask studentTask : studentTaskList){
            Task task = taskMapper.selectTaskById(studentTask.getTaskId());
            String checkinDescription = task.getCheckinDescription();
            String[] strItemIds = checkinDescription.split(","); // 使用逗号分割字符串
            int[] itemIds = new int[strItemIds.length]; // 创建一个整型数组来存储数字
            // 将分割出来的字符串转换为整数并存储到整型数组中
            for (int i = 0; i < strItemIds.length; i++) {
                itemIds[i] = Integer.parseInt(strItemIds[i]);
            }
            for(int itemId : itemIds){
                Item item = taskMapper.selectItemById(itemId);
                if(item.getIsPhoto() == 2){
                    continue;
                }CheckinImage checkinImage = taskMapper.selectCheckinImage(studentTask.getStudentId(), studentTask.getTaskId(), itemId);
                CheckinRequestDTO dto = new CheckinRequestDTO();
                dto.setId(studentTask.getId());
                dto.setImageId(checkinImage.getImageId());
                dto.setTaskId(studentTask.getTaskId());
                dto.setStudentId(studentTask.getStudentId());
                dto.setItemId(itemId);
                dto.setItemName(item.getItemName());
                dto.setImageUrl(SystemConstant.aliyunOSSUrlPrefix + checkinImage.getImageUrl());
                logs.add(dto);
            }
        }
        return new PageVo<>(pageBo, logs);
    }

    @Override
    public void updateCheckinStatus(int imageId, int recognitionStatus, int checkinStatus) {
        mapper.updateCheckinStatus(imageId, recognitionStatus, checkinStatus);
    }

    @Override
    public void updateCompletionStatus(int id, int completionStatus, int reviewStatus, int userId) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime completionTime = LocalDateTime.now();
        String completionTimeStr = completionTime.format(formatter);
        mapper.updateCompletionStatus(id, completionStatus, completionTimeStr, reviewStatus, userId);
    }

    @Override
    public void updateReviewerComments(int id, String reviewComments) {
        mapper.updateReviewerComments(id, reviewComments);
    }
}
